package example.mvpmode.util;


import android.content.Context;
import android.util.Log;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.cache.ExternalCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.module.GlideModule;
import com.bumptech.glide.module.LibraryGlideModule;

import java.io.File;

/**
 * Created by sunsh on 2017/12/21.
 */

@com.bumptech.glide.annotation.GlideModule
public class GlideConfiguration extends AppGlideModule {

    private static final int MEMORY_MAX_SPACE=(int) (Runtime.getRuntime().maxMemory()/8);

    public void applyOptions(Context context, GlideBuilder builder) {
        //设置加载图片的样式,比默认图片质量好,但占用内存会大点
        builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
        builder.setMemoryCache(new LruResourceCache(MEMORY_MAX_SPACE));
        builder.setDiskCache(new ExternalCacheDiskCacheFactory(context, getDiskFileString(context,"glideCache"), 30*1024*1024));

    }

    private String getDiskFileString(Context mContext,String str) {
        File dirFile=new File(mContext.getExternalCacheDir().getAbsolutePath().toString()+str);
        File tempFile=new File(dirFile,"bitmaps");
        if (! tempFile.getParentFile().exists()){
            tempFile.getParentFile().mkdirs();
        }
        Log.i("GlideConfiguration ",tempFile.getAbsolutePath().toString());
        return tempFile.getAbsolutePath().toString();
    }


    @Override
    public void registerComponents(Context context, Glide glide, Registry registry) {

    }
}
